#include <stdio.h>
#include <stdlib.h>

void PrintMatrixInCircle(int** numbers, int columns, int rows, int start);
void PrintMatrixClockwisely(int** numbers, int columns, int rows)
{
  if(numbers == NULL || columns <= 0 || rows <= 0)
  {
    return;
  }

  int start = 0;
  while(columns > start*2 && rows > start*2)
  {
    PrintMatrixInCircle(numbers, columns, rows, start);
    ++start;
  }
}

void PrintMatrixInCircle(int** numbers, int columns, int rows, int start)
{
  int end_X = columns - start - 1;
  int end_Y = rows - start - 1;

  // 从左到右打印一行
  for(int i = start; i <= end_X; i++)
  {
    int number = numbers[start][i];
    printf("%d ", number);
  }

  // 从上到下打印一列
  if(start < end_Y)
  {
    for(int i = start + 1; i <= end_Y; i++)
    {
      int number = numbers[i][end_X];
      printf("%d ", number);
    }
  }

  // 从右到左打印一行
  if(start < end_X && start < end_Y)
  {
    for(int i = end_X - 1; i >= start; i--)
    {
      int number = numbers[end_Y][i];
      printf("%d ", number);
    }
  }

  // 从下到上打印一列
  if(start < end_X && start < end_Y - 1)
  {
    for(int i = end_Y - 1; i >= start + 1; i--)
    {
      int number = numbers[i][start];
      printf("%d ", number);
    }
  }
}

int main(int argc, char const *argv[])
{
  int a[3][4] = {  
    {0, 1, 2, 3} ,   /*  初始化索引号为 0 的行 */
    {4, 5, 6, 7} ,   /*  初始化索引号为 1 的行 */
    {8, 9, 10, 11}   /*  初始化索引号为 2 的行 */
  };

  int** matrix = new int*[3];
  for(int row = 0; row < 3; row++)
  {
    for(int column = 0; column < 4; column++)
    {
      matrix[row] = a[row];
    }
  }

  PrintMatrixClockwisely(matrix, 4, 3);
  printf("\n");

  return 0;
}

